#!/usr/bin/env bash

# set -e: exit asap if a command exits with a non-zero status
# set -x: print each command right before it is executed
set -xe

set +x
echo "#================================"
echo "# Scenario 1a [basic]: Setup mock"
echo "#================================"
set -x

[ -z "${ADDED_TEST_SLEEP}" ] && export ADDED_TEST_SLEEP="0.5"

docker tag selenium:latest elgalu/selenium:latest

# Some dummy log files
rm -rf **/*"conflicted copy"*.log

# function get_mock_port() {
#   echo $(docker inspect -f='{{(index (index .NetworkSettings.Ports "8280/tcp") 0).HostPort}}' grid_mock)
# }

function get_mock_ip() {
  docker inspect -f='{{.NetworkSettings.IPAddress}}' grid_mock
}

docker rm -vf grid_mock || true

export MOCK_SERVER_PORT=8280

# Let's bind to all host interfaces in case the host has ufw enabled
docker run -d -t --name=grid_mock -e MOCK_SERVER_PORT \
  -p 0.0.0.0:$MOCK_SERVER_PORT:$MOCK_SERVER_PORT \
  elgalu/google_adwords_mock

docker attach grid_mock &

export MOCK_SERVER_HOST=$(get_mock_ip)

while ! curl -s "http://localhost:${MOCK_SERVER_PORT}/adwords"; do
  echo -n '.'
  sleep 0.2
done

set +x
echo "#==================================================================="
echo "# Scenario 1a [basic]: SETUP: Run tests right after the cont started"
echo "#==================================================================="
set -x

docker rm -vf grid || true

docker run --name=grid -d -e VIDEO=true \
  -e MOCK_SERVER_HOST \
  -e MOCK_SERVER_PORT \
  -e CI \
  -e VIDEO_CONVERSION_MAX_WAIT="40s" \
  -v /dev/shm:/dev/shm \
  --privileged \
  selenium

docker logs grid
docker attach grid &

# sleep ${ADDED_TEST_SLEEP} && docker logs grid
docker exec grid wait_all_done 70s
docker exec grid versions
docker exec grid errors || true
docker logs grid

# Test videos
docker exec grid stop-video
docker logs grid
docker exec -t grid log "-- DEBUG: video-rec-stdout.log ----"
docker exec -t grid tail -n 40 /var/log/cont/video-rec-stdout.log || true
docker exec -t grid log "-- DEBUG: video-rec-stderr.log ----"
docker exec -t grid tail -n 40 /var/log/cont/video-rec-stderr.log || true

set +x
echo "#==================================================================="
echo "# Scenario 1b [basic]: Run tests right after the container started"
echo "#==================================================================="
set -x

if ! docker exec -t grid run_test; then
  docker logs grid
  docker exec grid errors || true
  exit 103
fi

docker exec grid errors || true
docker cp grid:/test/console.png ./images/grid3_console.png
docker cp grid:/capabilities.json .

set +x
echo "#=================================================="
echo "# Scenario 1c [basic]: Make sure guest.docker works"
echo "#=================================================="
set -x

docker exec grid curl localhost:24444/grid/console
docker exec grid curl d.guest.loc.dev:24444/grid/console
docker exec grid curl guest.docker:24444/grid/console

set +x
echo "#======================================================"
echo "# Scenario 1d [basic]: Make sure host.docker works #177"
echo "#======================================================"
set -x

BASIC_WEB_SERVER_PORT=8710

docker stop basic_webserver || true
docker rm basic_webserver || true

# Let's bind to all host interfaces in case the host has ufw enabled
docker run -d -t --name=basic_webserver \
  -p 0.0.0.0:${BASIC_WEB_SERVER_PORT}:80 \
  nginx:1.15.11

docker attach basic_webserver &

# Wait for the basic_webserver to be ready
while ! curl -s "http://localhost:${BASIC_WEB_SERVER_PORT}"; do
  echo -n '.'
  sleep 0.2
done

# sudo vim /etc/default/ufw
# DEFAULT_FORWARD_POLICY="ACCEPT"
# sudo ufw reload
# sudo iptables -I INPUT -i docker0 -j ACCEPT
docker exec grid curl d.host.loc.dev:${BASIC_WEB_SERVER_PORT}
docker exec grid curl host.docker:${BASIC_WEB_SERVER_PORT}

docker stop basic_webserver
docker rm basic_webserver
